// pages/car/car.js
const app = getApp()
Page({
  data: {
    isAllSelect: false,
    totalMoney: 0,
    num: 1,
    price:0,
    prices:0.01,
    // 商品详情介绍
    carts: [
      {
        pic: "../image/order_list_01_03.png",
        name: "2018年最新村“两委”换届啊空间风格较大付款党换届啊空间风格较换届啊空间风格较大付款党",
        price: 89.90,
        isSelect: false,
        num: 1,
        // 数据设定
        count: {
          quantity: 1,
          min: 1,
          max: 20
        },
      },
    ],
  },
  // 增加数量
  addCount(e) {
    let that = this;
    var index = e.currentTarget.dataset.index;
    var carts = that.data.carts;
    carts[0].goods[index].num = carts[0].goods[index].num+1
    console.log(carts[0].goods[index].num + 1)
    that.setData({
      carts: carts
    })
    wx.request({
      url: app.data.Url + 'cart/edit',
      data: {
        cartid: e.currentTarget.id,
        type: 'plus'
      },
      method: 'post',
      success: function (res) {
        console.log(res)
      }
    })
    that.price()
  },
  // 减少数量
  minusCount(e) {
    let that = this;
    var index = e.currentTarget.dataset.index;
    var carts = that.data.carts;
    if (carts[0].goods[index].num <= 1){
      return false
    }else{
      carts[0].goods[index].num = carts[0].goods[index].num -1
      console.log(carts[0].goods[index].num - 1)
      that.setData({
        carts: carts
      })
      console.log()
      wx.request({
        url: app.data.Url + 'cart/edit',
        data: {
          cartid: e.currentTarget.id,
          type: 'cut'
        },
        method: 'post',
        success: function (res) {
          console.log(res)
        }
      })
    }
    
    that.price()
  },

  //选择
  switchSelect:function(e){
    let that = this;
    var index = e.currentTarget.dataset.index;
    var carts = that.data.carts;
    carts[0].goods[index].status = !carts[0].goods[index].status
    that.setData({
      carts: carts
    })
    console.log(that.data.carts)
    that.price()
  },
  //删除
  del:function(){
    let that = this;
    var carts = that.data.carts;
    var arr = ''
    for (var i = 0; i < carts[0].goods.length;i++){
      if (carts[0].goods[i].status == true){
        arr += carts[0].goods[i].cartid+','
      }else{
        console.log('------')
      }
    }
    wx.request({
      url: app.data.Url +'Cart/delete',
      data: {
        cartid: arr,
        uid:wx.getStorageSync('userInfo').uid
      },
      method: 'get',
      success: function (res) {
        console.log(res.data.data[0])
        that.setData({
          carts: res.data.data,
          arr: arr
        })
      }
    })
    that.price()
  },


  //全选
  allSelect: function (e) {
    let that = this;
    var carts = that.data.carts;
    var isAllSelect = !that.data.isAllSelect;
    if (isAllSelect){
      for (var i = 0; i < carts[0].goods.length; i++) {
        console.log(i)
        carts[0].goods[i].status = true
      }
      that.setData({
        carts: carts
      })
      //console.log(that.data.carts[0].goods)
    }else{
      for (var i = 0; i < carts[0].goods.length; i++) {
        carts[0].goods[i].status = false
      }
      that.setData({
        carts: carts
      })
    }
    that.setData({
      isAllSelect: isAllSelect
    })
    that.price()
  },

  //总价
  price:function(){
    console.log('执行函数')
    let that = this;
    var carts = that.data.carts;
    var p = 0;
    var cartid = []
    for (var i = 0; i < carts[0].goods.length; i++) {
      var s = carts[0].goods[i].min_price * carts[0].goods[i].num
      console.log(typeof s)
      //console.log(carts[0].goods[i].min_price * carts[0].goods[i].num)
      carts[0].goods[i].min = carts[0].goods[i].min_price * carts[0].goods[i].num
      if (carts[0].goods[i].status == true){
        p = p + parseInt(carts[0].goods[i].min)
        cartid.push(carts[0].goods[i].cartid)
      }else{

      }
      that.setData({
        price: p,
        carts: carts,
        cartid: cartid
      })
      //carts[0].goods[i].status = true
    }

    
  },
  // 去结算
  toBuy() {
    wx.showToast({
      title: '去结算',
      icon: 'success',
      duration: 3000
    }); wx.showToast({
      title: '收藏成功！',
    })
    this.setData({
      showDialog: !this.data.showDialog
    });
  },
  //数量变化处理
  handleQuantityChange(e) {
    var componentId = e.componentId;
    console.log(componentId)
    var quantity = e.quantity;
    this.data.carts[componentId].count.quantity = quantity;
    this.setData({
      carts: this.data.carts,
    });
  },
  onLoad:function(){
    let that=this;
    wx.request({
      url: app.data.Url + 'cart/cartLists',
      data:{
        uid:wx.getStorageSync('userInfo').uid
      },
      method:'get',
      success:function(res){
        //console.log(res.data.data[0].goods)
        var carts = res.data.data;
        //console.log(carts[0].goods.length)
        for (var i = 0; i <= carts[0].goods.length-1; i++) {
          //console.log(i)
          carts[0].goods[i].min = 0
        }
        that.setData({
          carts: carts
        })
        console.log(res.data.data)
      }
    })
  },
  onShow:function(){
    let that = this;
    wx.request({
      url: app.data.Url + 'cart/cartLists',
      data: {
        uid: wx.getStorageSync('userInfo').uid
      },
      method: 'get',
      success: function (res) {
        //console.log(res.data.data[0].goods)
        var carts = res.data.data;
        //console.log(carts[0].goods.length)
        for (var i = 0; i <= carts[0].goods.length - 1; i++) {
          //console.log(i)
          carts[0].goods[i].min = 0
        }
        that.setData({
          carts: carts
        })
        console.log(res.data.data)
      }
    })
  },
  jiesuan:function(){
    let that=this;
    if (that.data.cartid == '' || that.data.cartid==undefined){
      wx.showToast({
        title: '请选择商品',
      })
    }else{
      wx.navigateTo({
        url: '../confirms/confirm?money=' + that.data.price + '&cartid=' + that.data.cartid,
      })
    }
  }
});
